<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div>TODO write content</div>
        <script type="text/javascript">
            //this总是指向它的调用者
            window.name = "window";
            var run = function() {
                //全局调用函数时，this指向DOMWindow
                alert("My name is " + this.name);
            }
            //run();
            var object = {
                name: "object", 
                run: function() {
                    //this指向调用者object
                    alert("My name is " + this.name);
                }
            };
            //object.run();
            
            var object2 = {
                name: "object2",
                run: function() {
                    var inner = function(){
                        //inner既不是object的属性也不是run的属性,松散在存在于run的作用域用，不能被前两者调用，就只有被window调用
                        //this指向DOMWindow
                        alert("My name is " + this.name);
                    }
                    inner();
                }
            };
            //object2.run();
            
            var object3 = {
                name: "object3",
                run: function() {
                    var inner = function(){
                        //this指向object3
                        alert("My name is " + this.name);
                    }
                    //Call:它的第一个参数用作 this 的对象。其他参数都直接传递给函数自身。
                    inner.call(this);
                }
            };
            //object3.run();
            
            var cat = {
                name: "Cat"
            };
            var dog = {
                name: "Dog",
                sound: function(word) {
                    alert(this.name + word);
                }
            };
 
            //dog.sound(" is pooping");  //this指向dog
            //dog.sound.call(window, " is banking"); //this指向window 
            //dog.sound.call(dog, " is banking"); //this指向dog
            //dog.sound.apply(cat, [" miaowing"]); //this指向cat
            
            //bind函数
            //简单版本
            var bind = function(context, fn) {
                return function() {
                    return fn.apply(context, arguments);
                }
            }
            //复杂版本,绑定时能够传递参数
            function bind2(context,fn) {
                var args = Array.prototype.slice.call(arguments, 2);
                return args.length == 0 ? function() {
                    return fn.apply(context, arguments);
                } : function() {
                    return fn.apply(context, args.concat.apply(args, arguments));
                };
            };

            //var sound = bind(window,dog.sound); //将dog.sound函数内的this对象指向window
            var sound = bind2(window,dog.sound," is wangwang");
            sound(" is banking");
        </script>
    </body>
</html>
